<?php

class EncuestasController extends GxController {

    private $enruta = 'view';

    public function filters() {
        return array(
            'accessControl',
        );
    }

    public function accessRules() {
        return array(
            array('allow',
                'actions' => array('index', 'view'),
                'users' => array('*'),
            ),
            array('allow',
                'actions' => array('minicreate', 'create', 'update'),
                'users' => array('@'),
            ),
            array('allow',
                'actions' => array('admin', 'delete'),
                'users' => array('admin'),
            ),
            array('deny',
                'users' => array('*'),
            ),
        );
    }

    public function actionView($id, $asiste = null) {
        Controller::registrar_scrips();
        Controller::registrar_scrips_jquery();
        if ($asiste == 1) {
            $this->layout = '//layouts/column3';
        }
        $this->render('view', array(
            'model' => $this->loadModel($id, 'Encuestas'),
        ));
    }

    public function actionCreate($asiste = null, $acordeon = null) {
        $model = new Encuestas;
        $this->performAjaxValidation($model, 'encuestas-form');
        $parametros = array();
        Controller::registrar_scrips();
        Controller::registrar_scrips_jquery();
        if ($asiste == 1) {
            $this->layout = '//layouts/column3';
            $this->enruta = "admin";
        }
        $parametros[0] = $this->enruta;
        $parametros["asiste"] = $asiste;
        $parametros["acordeon"] = $acordeon;
        if (isset($_POST['Encuestas'])) {
            $model->setAttributes($_POST['Encuestas']);
            if ($model->save()) {
                $parametros["id"] = $model->ENCID;
                $parametros["creadoAsiste"] = ($asiste == 1) ? "Si" : "No";
                if (Yii::app()->getRequest()->getIsAjaxRequest())
                    Yii::app()->end();
                else
                    $this->redirect($parametros);
            }
        }

        $this->render('create', array('model' => $model));
    }

    public function actionUpdate($id, $asiste = null, $acordeon = null) {
        $model = $this->loadModel($id, 'Encuestas');
        $this->performAjaxValidation($model, 'encuestas-form');
        $parametros = array();
        Controller::registrar_scrips();
        Controller::registrar_scrips_jquery();
        if ($asiste == 1) {
            $this->layout = '//layouts/column3';
            $this->enruta = "admin";
        }
        $parametros[0] = $this->enruta;
        $parametros["asiste"] = $asiste;
        $parametros["acordeon"] = $acordeon;
        $parametros["id"] = $id;
        if (isset($_POST['Encuestas'])) {
            $model->setAttributes($_POST['Encuestas']);
            if ($model->save()) {
                $this->redirect($parametros);
            }
        }
        $this->render('update', array(
            'model' => $model,
        ));
    }

    public function actionDelete($id) {
        Controller::registrar_scrips();
        if (Yii::app()->getRequest()->getIsPostRequest()) {
            $this->loadModel($id, 'Encuestas')->delete();

            if (!Yii::app()->getRequest()->getIsAjaxRequest())
                $this->redirect(array('admin'));
        } else
            throw new CHttpException(400, Yii::t('app', 'Your request is invalid.'));
    }

    public function actionIndex($asiste = null) {
        Controller::registrar_scrips();
        Controller::registrar_scrips_jquery();
        if ($asiste == 1) {
            $this->layout = '//layouts/column3';
        }
        $dataProvider = new CActiveDataProvider('Encuestas');
        $this->render('index', array(
            'dataProvider' => $dataProvider,
        ));
    }

    public function actionAdmin($asiste = null) {
        Controller::registrar_scrips();
        Controller::registrar_scrips_jquery();
        if ($asiste == 1) {
            $this->layout = '//layouts/column3';
        }
        $model = new Encuestas('search');
        $model->unsetAttributes();
        if (isset($_GET['Encuestas']))
            $model->setAttributes($_GET['Encuestas']);
        $this->render('admin', array(
            'model' => $model,
        ));
    }

}